﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 反射2
{
    class Program
    {
        static void Main(string[] args)
        {
            Type t1 = typeof(Person);
            Type t1Type = t1.GetType();
            //BaseType 获取父类
            Console.WriteLine("获取父类："+t1Type.BaseType);
            Console.WriteLine("获取父类的父类："+t1Type.BaseType.BaseType);
            //FullName 获取全名
            Console.WriteLine("获取全名："+t1.FullName);
            //IsAbstract 是否抽象类
            Console.WriteLine("是否抽象类："+t1.IsAbstract);
            //IsEnum 是否枚举
            Type te = typeof(MyEnum);
            Console.WriteLine("是否枚举："+te.IsEnum);
            // 是否数组
            int[] nums=  { 1, 2, 3 };
            Type tn = nums.GetType();
            Console.WriteLine("是否数组：" +tn.IsArray);
            //获取无参构造函数
            ConstructorInfo c1 = t1.GetConstructor(new Type[0]);
            Console.WriteLine("获取无参构造函数"+c1);
            //获取有参构造函数
            ConstructorInfo c2 = t1.GetConstructor(new Type[] { typeof(string) });
            Console.WriteLine("获取有参构造函数" + c2);
            ConstructorInfo c3 = t1.GetConstructor(new Type[] {typeof(int), typeof(string) });
            Console.WriteLine("获取有参构造函数" + c3);
            //获取字段
            FieldInfo[] fields = t1.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                Console.WriteLine("获取所有字段："+field);
            }
            //获取方法
            MethodInfo[] methods = t1.GetMethods();
            foreach (MethodInfo method in methods)
            {
                Console.WriteLine("获取所有的方法："+method);
            }
            //根据名字获取方法
            MethodInfo method1 = t1.GetMethod("SayHi",new Type[0]);
            Console.WriteLine("根据名字获取方法："+method1);
            MethodInfo method2 = t1.GetMethod("SayHi",new Type[] { typeof(string) });
            Console.WriteLine("根据名字获取带参数的方法：" + method2);

            //获取属性
            PropertyInfo[] properties = t1.GetProperties();
            foreach (PropertyInfo item in properties)
            {
                Console.WriteLine("获取属性："+item);
            }
            PropertyInfo prop = t1.GetProperty("Name");
            Console.WriteLine(prop);


        Console.ReadKey();
        }


        enum MyEnum
        {
            abc,
            bcd,
            cde
        }
    }

    class Person
    {
        public Person()
        {

        }

        public Person(string name)
        {

        }
        public Person(int age,string name)
        {

        }

        public string address;
        private string mobile;
        public string Name { get; set; }

        public int Age { get; set; }

        public void SayHi()
        {
            Console.WriteLine("大家好，我是"+this.Name);
        }
        public void SayHi(string s)
        {
            Console.WriteLine("大家好，我是" + s);
        }

    }
}
